package com.sgb.member.view_model;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.QualityMemberBean;
import com.sgb.member.ui.fragment.HomeQualityMemberFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.aroute.SimpleARouteCallback;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

import retrofit2.Call;

public class QualityMemberModel extends BaseViewModel {

    public MutableLiveData<Boolean> dialog = new MutableLiveData<>();
    private HomeQualityMemberFragment fragment;
    private MemberRepertory mRepertory;

    public QualityMemberModel(HomeQualityMemberFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.mRepertory = repertory;
    }

    private void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
            fragment.closeRecyclerRefreshing();
        }
    }

    /**
     * 优质企业会员
     */
    public void getRecommendedMembers() {
        final String[] longitude = {SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "")};
        final String[] latitude = {SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "")};
        if (TextUtils.isEmpty(longitude[0]) || "0.0".equals(longitude[0])) {
//            MemberConfig.getInstance().getLocalPosition(fragment.getContext(), new SimpleARouteCallback() {
//                @Override
//                public void onResult(Object... objects) {
//                    if (objects != null) {
//                        longitude[0] = String.valueOf(objects[0]);
//                        latitude[0] = String.valueOf(objects[1]);
//                        getRemdData(longitude[0], latitude[0]);
//                    } else {
                        DialogHelper.dismissProgressDialog();
                        fragment.showEmpty();
//                    }
//                }
//            });
        } else {
            getRemdData(longitude[0], latitude[0]);
        }
    }

    private void getRemdData(String lng, String lat) {
        showOrHideDialog(true);
        mRepertory.getApi().getRecommendedMembers(mRepertory.getAreaCode(), lng, lat).enqueue(new ICallBack<BaseEntity<List<QualityMemberBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<QualityMemberBean>> data) {
                fragment.refreshRecommendedRecycler(data.getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<QualityMemberBean>>> call, Throwable t) {
                super.onFailure(call, t);
                fragment.showEmpty();
            }

            @Override
            public void onFinally() {
                showOrHideDialog(false);

            }
        });
    }
}
